package sushil.luc.ticket;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.content.Context;
import android.util.Log;

public class TicketManagerTrucker implements TicketManager{
	
	private Context context;
	private TicketService ticketserv;
	public static List<Ticket> ticketlist;

	
	public TicketManagerTrucker(Context parent)
	{
			this.context =parent;
			ticketserv = new TicketService();
	}
	
	/**
	 * Order the tickets in such a way that the ticket with the soonest delivery date are first
	 */
	@Override
	public List<Ticket> orderTickets(List<Ticket> alltickets) {	
		// define a comparator which compares the deliverydate of the tickets
		Comparator<Ticket> comp = new Comparator<Ticket>() {			
			@Override
			public int compare(Ticket lhs, Ticket rhs) {
				return lhs.getDeliveryDate().compareTo(rhs.getDeliveryDate());
			}
		};
    	// sort the tickets with the comparator
    	Collections.sort(alltickets,comp);
    	ticketlist = alltickets;
    	return ticketlist;
	}
	
	public void getOptimalCustomerRoute()
	{
		// Not possible since there is only one customer per trucker...
	}
	
	/**
	 * Get all the Tickets which have the status checked
	 * @return
	 */
	public List<Ticket> fetchTickets()
	{
		ticketlist = ticketserv.fetchTruckerTickets(context);
		Log.d("TicketManager", "ticketlist"+ticketlist.size());
    	return orderTickets(ticketlist);
	}
	
	public List<Ticket> fetchLocalTickets()
	{
		return orderTickets(ticketserv.getLocalTruckerTickets());
	}
	
	/**
	 * set all Items of the ticket to staged on the server
	 * @param t
	 */
	public void stageAllItems(Ticket t)
	{
		ticketserv.updateTicketStaged(t);
	}
}
